package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.common.lib.constant.EventBusEvent;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 返回按钮（用于在第三方应用中返回本应用）
 */
public class CountDownViewUtils {

    private static CountDownViewUtils countDownViewUtils;
    WindowManager windowManager;
    Context mContext;
    boolean isFist = true;
    View inflate;
    boolean isVideo = false;
    Disposable mDisposableCountDown;
    TextView tvCount;

    public static CountDownViewUtils getInstance(){
        if(countDownViewUtils == null){
            countDownViewUtils = new CountDownViewUtils();
        }
        return countDownViewUtils;
    }

    public void initView(Context context,int time){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_count_down, null);
            tvCount = inflate.findViewById(R.id.tvCount);
        }
//        tvCount.setText(time+"");
        startCountDown(time);


        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.CENTER;

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }

    HashMap<String, Object> map = new HashMap<>();
    public void startCountDown(int time){
        if(mDisposableCountDown != null){
            return;
        }
        closeDisposableCountDown();
        Observable.interval(1,  TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   跳转到视频界面");

                        tvCount.setText(time+"");
                    }
                })
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposableCountDown = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        long l = aLong + 1;
                        String srt = null;
                        if((time - l) <= 0){
                            srt = "GO";
                        }else {
                            srt = String.valueOf(time - l);
                        }
                        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   time :   "+srt);
                        tvCount.setText(srt);
                        if((time - l) == 1){
                            map.clear();
                            if(isVideo){
                                map.put(EventBusEvent.TO_VIDEO, isVideo);
                            }else {
                                map.put(EventBusEvent.IS_SHOW_COUNT,0);
                            }
                            EventBus.getDefault().post(map);
                        }
                        if ((time - l) == 0) {
                            closeDisposableCountDown();
                            Log.e("aaaaaaaaaaaaa", "start run");
//                            map.clear();
//                            if(isVideo){
//                                map.put(EventBusEvent.TO_VIDEO, isVideo);
//                            }else {
//                                map.put(EventBusEvent.IS_SHOW_COUNT,0);
//                            }
//                            EventBus.getDefault().post(map);
                            if(DeviceTypeUtils.connectDeviceType() == 0){
                                System.out.println("ddddddddddddddddddddddd   蓝牙通知设备开始");
                                if(DeviceTypeUtils.getInstance().isTreadmill()){
                                    System.out.println("ddddddddddddddddddddddd   蓝牙通知跑步机设备开始");
//                                    BluetoothOrder.getInstance().startTreadmill();
                                }else {
                                    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    6666");
                                    BluetoothOrder.getInstance().startPlay();
                                }

                            }else {
                                System.out.println("ddddddddddddddddddddddd   串口通知设备开始");
                                SerialPortManager.getInstance().startRun();
                            }
                            SportsStatusBarViewUtils.getInstance().showStartBtn(true);
                            goneLayout();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        closeDisposableCountDown();
                    }

                    @Override
                    public void onComplete() {
                        closeDisposableCountDown();
                    }
                });
    }

    public void closeDisposableCountDown(){
        if (mDisposableCountDown != null) {
            mDisposableCountDown.dispose();
            mDisposableCountDown = null;
        }
    }


    public void setIsVideo(boolean b){
        isVideo = b;
    }

    public void goneLayout(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
                closeDisposableCountDown();
            }
        }

    }
    public void invaliView(){
        if(inflate != null){
            windowManager.removeView(inflate);
            if (mDisposableCountDown != null) {
                mDisposableCountDown.dispose();
                mDisposableCountDown = null;
            }
            isFist = true;
            isVideo = false;
            windowManager = null;
            inflate = null;

        }
    }

}
